//
//  NewsDetailViewController.m
//  News
//
//  Created by wangshan on 16/2/28.
//  Copyright © 2016年 News. All rights reserved.
//

#import "NewsDetailViewController.h"
#import "NewsModel.h"
#import "FMDatabase+Database.h"
#import "UIViewController+Notice.h"
#import "ShowViewController.h"
#import "CollectViewController.h"
#import <MBProgressHUD.h>

@interface NewsDetailViewController ()<UIWebViewDelegate>


@property (nonatomic,copy) NSString *dateString;
@property (strong, nonatomic) IBOutlet UIWebView *detailWeb;

- (IBAction)backAction:(UIBarButtonItem *)sender;
- (IBAction)collectionAndShare:(UIBarButtonItem *)sender;

@end

@implementation NewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.translucent = NO;
    
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height +44+ 64;
    self.detailWeb.scrollView.hidden = YES;
    self.detailWeb.frame = CGRectMake(0, -54, width, height);
    
    NSString *link = [NSString stringWithFormat:@"http://sinanews.sina.cn/sinanews.shtml?id=%@",self.newsModel.ID];
    NSLog(@"%@",link);
    self.detailWeb.delegate = self;
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:link]];
    [self.detailWeb loadRequest:request];
    self.detailWeb.scrollView.bounces = NO;
    self.detailWeb.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.detailWeb];
    
    self.detailWeb.opaque = NO;
    [self changeBackgroung];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    self.dateString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.newsModel.articlePubDate]];
    NSLog(@"%@",self.dateString);
    
}

- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    
    
    [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML;"];

    [webView stringByEvaluatingJavaScriptFromString:@"var header1 = document.body.getElementsByTagName('section')[1];header1.parentNode.removeChild(header1);var footer = document.body.getElementsByTagName('footer')[0];footer.parentNode.removeChild(footer);var header2 = document.body.getElementsByTagName('section')[0];header2.parentNode.removeChild(header2);document.body.innerHTML;"];

    [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML;"];

    
    [webView stringByEvaluatingJavaScriptFromString:@"var header3 = document.body.getElementsByTagName('section')[0];var header4 = header3.getElementsByTagName('section')[1];header4.parentNode.removeChild(header4);"];

    
    
    NSMutableString *js1 = [NSMutableString string];
    [js1 appendString:@"var header1 = document.body.getElementsByTagName('section')[1];var header2 = header1.getElementsByTagName('section')[1];document.body.innerHTML;"];
        
        [webView stringByEvaluatingJavaScriptFromString:js1];
    
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"var header5 = document.body.getElementsByTagName('h6')[0];header5.parentNode.removeChild(header5)"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
        webView.scrollView.hidden = NO;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
}

//通知
-(void)changeBackgroung
{
    
    extern BOOL isNightMode;
    if (isNightMode) {
        
        self.view.backgroundColor = [UIColor darkGrayColor];
    }else {
        
        self.view.backgroundColor = [UIColor whiteColor];
    }
}



- (IBAction)backAction:(UIBarButtonItem *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)collectionAndShare:(UIBarButtonItem *)sender {
    
    CollectViewController *collctionAndShareVC = [[UIStoryboard storyboardWithName:@"Collect" bundle:nil] instantiateViewControllerWithIdentifier:@"collect"];
    collctionAndShareVC.newsModel = self.newsModel;
    
    // 第三方分享
    collctionAndShareVC.pictureLink = self.newsModel.link;
    collctionAndShareVC.titShare = self.newsModel.title;
    
    [self.view addSubview:collctionAndShareVC.view];
    [self addChildViewController:collctionAndShareVC];
    
}
@end
